﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Jy123.Common;

namespace Jy123.Web.Module.Club
{
    public class Club_BuyCoche : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.QueryString["Type"];
            if (string.IsNullOrEmpty(type))
            {
                type = context.Request["Type"];
            }
            string cIdString;
            BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
            if (type.ToLower().Equals("load"))
            {
                cIdString = context.Request.QueryString["ClubId"];
                if (!string.IsNullOrEmpty(cIdString))
                {
                    int cId = int.Parse(cIdString);
                    Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                    Jy123.Model.Club_Main club = BLLClub.GetModel(cId);
                    BLL.Public_JType BLLJType = new Jy123.BLL.Public_JType();
                    Model.Public_JType JType = BLLJType.GetModel(club.FJType.Value);                    
                    Model.User_Main user = BLLUser.GetModel(club.FUser.Value);
                    StringBuilder sbString = new StringBuilder();
                    Jy123.BLL.Public_Star BLLStar = new Jy123.BLL.Public_Star();
                    List<Jy123.Model.Public_Star> listStar = BLLStar.GetModelList("FSType = 2 AND FSportType = " + JType.FSportType.Value.ToString());
                    BLL.Club_StarRecord BLLRecord = new Jy123.BLL.Club_StarRecord();
                    List<Model.Club_StarRecord> listRecord = BLLRecord.GetModelList("FClub = " + cIdString);
                    foreach (Jy123.Model.Public_Star cStar in listStar)
                    {
                        string strStar = context.Cache["clubbuycoche"].ToString();
                        strStar = strStar.Replace("$CocheImage$", cStar.Model);
                        strStar = strStar.Replace("$CocheName$", cStar.SName);
                        strStar = strStar.Replace("$CocheDescription$", cStar.Description);
                        strStar = strStar.Replace("$Skill$", cStar.Skill.Value.ToString());
                        strStar = strStar.Replace("$Physical$", cStar.PhysicalFitness.Value.ToString());
                        strStar = strStar.Replace("$Liveness$", cStar.Liveness.Value.ToString());
                        strStar = strStar.Replace("$Moral$", cStar.Moral.Value.ToString());
                        strStar = strStar.Replace("$Exp$", cStar.Experience.Value.ToString());
                        string strClick = string.Format("PostAjax('/Module/Club/Club_BuyCoche.ashx','Type=Buy&ClubId={0}&StarId={1}');fillhtml('clubInfoDiv','/Module/Club/Club_BuyCoche.ashx?Type=Load&ClubId='+$('#venuesHidden').val()+'&'+ Math.random());", cIdString, cStar.ID.ToString());
                        string strClass = "BtnBlueBg";
                        string strValue = "购买教练";
                        if (IsInList(cStar, listRecord))
                        {
                            strClass = "BtnBlueGray";
                            strValue = "已购教练";
                            strClick = "$.moonbox.warning('您已经购买了这位教练！')";
                        }
                        else if (user.Cash.Value < cStar.Price.Value)
                        {
                            strClick = "$.moonbox.warning('您的现金不足！')";
                        }
                        else if (!cStar.UsingLevel.Contains(club.FLevel.Value.ToString()))
                        {
                            strClass = "BtnBlueGray";
                            strValue = "等级不够";
                            strClick = "$.moonbox.warning('这位教练不适合您的俱乐部！')";
                        }
                        strStar = strStar.Replace("$Class$", strClass);
                        strStar = strStar.Replace("$Value$", strValue);
                        strStar = strStar.Replace("$ClickEvent$", strClick);
                        sbString.Append(strStar);
                    }
                    if (sbString.Length > 0)
                    {
                        context.Response.Write(sbString.ToString());
                    }
                    else
                    {
                        context.Response.Write("暂无教练，敬请期待");
                    }
                }
            }
            else
            {
                cIdString = context.Request["ClubId"];
                string strStar = context.Request["StarId"];
                if (!string.IsNullOrEmpty(cIdString) && !string.IsNullOrEmpty(strStar))
                {
                    int cId = int.Parse(cIdString);
                    int sId = int.Parse(strStar);
                    int uId = int.Parse(Cookie.getCookie("UserID"));
                    BLL.Star.Star BLLStar = new Jy123.BLL.Star.Star();
                    BLLStar.BuyStar(uId, cId, sId);
                    string strValue = "0";
                    BLL.Club_Main BLLClub = new BLL.Club_Main();
                    Model.Club_Main club = BLLClub.GetModel(cId);
                    if (club.Assets.HasValue)
                    {
                        strValue = (Convert.ToInt32(club.Assets.Value)).ToString();
                    }
                    string strAssets = string.Format("总资产：<span class=\"FontRedB\">{0}</span>", strValue);                    
                    Model.User_Main user = BLLUser.GetModel(club.FUser.Value);
                    string strCash = string.Format("现金：<span class=\"FontRedB\">{0}</span>", user.Cash.Value.ToString());
                    StringBuilder sbString = new StringBuilder();
                    sbString.Append("<script type='text/javascript' language='javascript'>");
                    sbString.AppendFormat("$(\"#assetsLi\").html('{0}');", strAssets);
                    sbString.AppendFormat("$(\"#cashLi\").html('{0}');", strCash);
                    //sbString.Append("$(\"#cashLi\").html('0');");
                    sbString.Append("</script>");
                    context.Response.Write(sbString.ToString());
                }                
            }
        }

        private bool IsInList(Model.Public_Star star, List<Model.Club_StarRecord> listRecord)
        {
            foreach (Model.Club_StarRecord record in listRecord)
            {
                if (record.FStar.HasValue)
                {
                    if (record.FStar.Value == star.ID)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
